package com.codehacker.job.mallservice.controller;

import com.codehacker.job.mallservice.dao.OrderMapper;
import com.codehacker.job.mallservice.dto.ResponseDTO;
import com.codehacker.job.mallservice.entity.Order;
import com.codehacker.job.mallservice.entity.OrderExample;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * OrderController
 *
 * @author zhuyuwei
 * @date 26/07/2017
 */
@Controller
@RequestMapping("/api/order")
public class OrderController {
    @Resource
    private OrderMapper orderMapper;

    @GetMapping("")
    @ResponseBody
    public ResponseDTO<Order> pageing(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, @RequestParam("type") Integer type) {
        ResponseDTO<Order> result = new ResponseDTO<Order>();
        OrderExample example = new OrderExample();
        example.createCriteria().andStatusEqualTo(type);
        example.setOrderByClause("create_time desc");
        result.getData().getItems().addAll(orderMapper.selectByExampleWithPaging(example, (page - 1) * limit, limit));
        result.getData().getPaginate().setNext(page + 1);
        result.getData().getPaginate().setHasNext(result.getData().getPaginate().getTotal() < (page + 1)*limit ? false: true);
        result.getData().getPaginate().setPerPage(limit);
        result.getMeta().setCode(0);
        return result;
    }
}
